package cn.xenosp.server.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.user.entity.SysUserVO;
import cn.xenosp.server.module.user.service.SysUserService;
import cn.xenosp.server.module.userRole.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-08-25 13:10
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/user/")
public class UserController {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysUserRoleService sysUserRoleService;

    @LogInfo(operationModuleType = "用户模块",operationName = "分页查询",operationContent = "查询用户数据")
    @PostMapping("/getPage")
    public Result<?> getUserList(@RequestBody String body){
        try {
            SysUserVO sysUserVO = ObjectConvert.urlStringToClass(body, SysUserVO.class);
            return Result.OK("", sysUserService.getPage(sysUserVO));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "新增用户",operationContent = "新增用户")
    @PostMapping("/addUser")
    public Result<?> addUser(@RequestBody SysUserVO sysUserVO){
        try {
            sysUserService.addUser(sysUserVO);
            return Result.OK("添加成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "校验用户数据",operationContent = "校验用户数据")
    @PostMapping("/userVerify")
    public Result<?> userVerify(@RequestBody SysUserVO sysUserVO){
        try {
            return sysUserService.userVerify(sysUserVO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "更新用户",operationContent = "更新用户")
    @PostMapping("/updateUser")
    public Result<?> updateUser(@RequestBody SysUserVO sysUserVO){
        try {
            sysUserService.updateUser(sysUserVO);
            return Result.OK("更新成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "更新用户密码",operationContent = "更新用户密码")
    @PostMapping("/updatePassword")
    public Result<?> updatePassword(@RequestBody SysUserVO sysUserVO){
        try {
            sysUserService.updatePassword(sysUserVO);
            return Result.OK("更新成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "删除用户",operationContent = "删除用户")
    @PostMapping("/delUser")
    public Result<?> delUser(@RequestBody SysUserVO sysUserVO){
        try {
            if (StrUtil.isEmpty(sysUserVO.getId())){
                throw new NullPointerException();
            }
            if (sysUserVO.getId().contains(",")){
                sysUserService.delete(sysUserVO.getId().split(","));
            }else {
                sysUserService.delete(sysUserVO.getId());
            }
            return Result.OK("删除成功", null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "用户绑定角色",operationContent = "用户绑定角色")
    @PostMapping("/bindRole")
    public Result<?> bindResources(@RequestBody SysUserVO sysUserVO){
        try {
            sysUserRoleService.userBindRole(sysUserVO.getId(),sysUserVO.getRoleList());
            return Result.OK("分配成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败",null);
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "获取资源",operationContent = "查询用户拥有的资源")
    @PostMapping("/getResources")
    public Result<?> getResources(@RequestBody SysUserVO sysUserVO){
        try {
            List<Map<String,Object>> userMenuList = sysUserService.userGetResources(sysUserVO.getId());
            return Result.OK("获取成功",userMenuList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败",null);
        }
    }
    @LogInfo(operationModuleType = "用户模块",operationName = "获取角色",operationContent = "查询用户拥有的角色")
    @PostMapping("/getRole")
    public Result<?> getRole(@RequestBody SysUserVO sysUserVO){
        try {
            List<Map<String,Object>> userMenuList = sysUserService.userGetRole(sysUserVO.getId());
            return Result.OK("获取成功",userMenuList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败",null);
        }
    }
}
